/// beamnpc.txt
// A monster with a special ability scripted in. The ability is an beam, used 
// 50% of the time.

// Memory Cells:
//   Cell 0 - If 0, default behavior. If 1, it doesnt fidget.
//   Cell 1,2 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.
//   Cell 4 - The ability to use.
//   Cell 5 - talking

// The abilities:
// 0 - Beam Slow
// 1 - Beam Curse
// 2 - Beam Fire (1-6 dam per level)
// 3 - Beam Acid
// 4 - Beam Terror
// 5 - Beam Daze
// 6 - Beam Debuff
// 7 - Beam Cold (1-8 dam per level)
// 8 - Beam Energy (1-10 dam per level)
// 9 - Beam Charm
// 10 - unused
// 11 - Beam lightning

begincreaturescript;

variables;

short target;
short last_abil;
short used_abil = 0;
short which_beam = 0;
short last_spawn;
short did_talk = 0;
short heal_phase = 0;
short last_heal;
short heal_level = 300;
short r1;

body;

beginstate INIT_STATE;
	set_name(ME,"Sammann");
	set_level(ME,40);
	change_max_health(ME,200);
	set_boss_level(ME,2);
	set_attack_bonus(ME,20);
	set_new_abil(ME,8);

	set_resistance(ME,7,90);
	
	last_abil = get_current_tick();
	last_spawn = get_current_tick();
	last_heal = get_current_tick();


	break;

beginstate DEAD_STATE;
	sf(81,3,1);
	begin_talk_mode(22);
break;

beginstate START_STATE; 
	// if I have a target for some reason, go attack it
	if (target_ok()) {
		//if (dist_to_char(get_target()) <= 16)
			set_state(3);
		//	else set_foe_target(ME,-1);
		}
	
	if ((did_talk == 0) && (get_nearest_party_char(8) >= 0)) {
		did_talk = 1;
		talk_no_exit(10);
		}
		
	if (get_foe_target(ME,8,0)) {
		do_attack();
		set_state(3);
		}
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}

	if ((tick_difference(last_spawn,get_current_tick()) > 3) && (get_attitude(ME) >= 10) && (gf(81,2) > 0)) {
		r1 = get_ran(1,0,2);
		if ((get_memory_cell(3 + r1) > 0) && (char_ok(get_memory_cell(3 + r1)) == FALSE)) {
			if (spawn_creature(get_memory_cell(3 + r1) - 8)) {

				set_summon_level(get_memory_cell(3 + r1),1);
				place_particle_num(get_memory_cell(3 + r1),10,7,10);
				
				set_attack_bonus(get_memory_cell(3 + r1),8);
				
				run_char_animation(2,1,50);	
				pc_heard_sound_delay(136,100);						
				
				last_spawn = get_current_tick();
				end();
				}
			}
		}

	if ((get_attitude(ME) >= 10) && (gf(81,4) > 0)) {
		set_foe_target(ME,pc_num());
		set_state(3);
		}

	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if (target_ok() == FALSE)
		set_state(START_STATE);

	if ((did_talk == 0) && (get_nearest_party_char(10) >= 0)) {
		did_talk = 1;
		talk_no_exit(10);
		}

	used_abil = 0;
	sf(81,4,1);
	
	if ((can_see_char(get_target())) && (is_combat()) && (tick_difference(last_abil,get_current_tick()) > 0)) {
		run_char_animation(2,1,35);	

		if (which_beam == 0) { // beam slow
			which_beam = 1;
			print_named_str(ME,"casts a powerful curse.");
			set_char_status(get_target(),0,-15);
			set_char_status(get_target(),8,-15);
			set_char_status(get_target(),1,-15);
			set_char_status(get_target(),5,60);
			shoot_projectile(ME,get_target(),38);
			pc_heard_sound_delay(128,100);						
			used_abil = 1;
			}
		else if (which_beam == 1) { // beam slow
			which_beam = 2;
			print_named_str(ME,"fires a decaying beam.");
			set_char_status(get_target(),20,25);
			damage_char(get_target(),get_ran(1,400,500),5);
			pc_heard_sound_delay(177,100);						
			shoot_projectile(ME,get_target(),32);
			used_abil = 1;
			}
		else if (which_beam == 2) { // beam fire
			which_beam = 0;
			print_named_str(ME,"flings a lance of fire.");
		  	place_particle_num(get_target(),2,4,8);
			damage_char(get_target(),get_ran(1,500,600),2);
			pc_heard_sound_delay(178,100);						
			shoot_projectile(ME,get_target(),31);
			used_abil = 1;
			}


		last_abil = get_current_tick();
		end();
		}

	if ((tick_difference(last_spawn,get_current_tick()) > 0) && (get_attitude(ME) >= 10)) {
		r1 = get_ran(1,0,2);
		if ((get_memory_cell(3 + r1) > 0) && (char_ok(get_memory_cell(3 + r1)) == FALSE)) {
			if (spawn_creature(get_memory_cell(3 + r1) - 8)) {
				print_named_str(ME,"mutters a spell. His machinery makes a new creation!");
				set_summon_level(get_memory_cell(3 + r1),1);
				place_particle_num(get_memory_cell(3 + r1),10,7,10);
				set_boss_level(get_memory_cell(3 + r1),1);
				set_new_abil(get_memory_cell(3 + r1),20);
				set_resistance(get_memory_cell(3 + r1),7,50);
				
				set_attack_bonus(get_memory_cell(3 + r1),8);
				
				run_char_animation(2,1,50);	
				pc_heard_sound_delay(136,100);						
				
				last_spawn = get_current_tick();
				end();
				}
			}
		}
		
	if ((heal_phase == 0) && (get_health(ME) < (get_max_health(ME) * 3) / 4)) {
		heal_phase = 1;
		print_str_color("Sammann mutters a spell. The color of his skin changes slightly.",3);
		print_str_color("  It seems to now have less of a green tinge.",3);
		}
	else if ((heal_phase == 1) && (get_health(ME) < (get_max_health(ME) * 2) / 4)) {
		heal_phase = 2;
		print_str_color("Sammann mutters a spell. The color of his skin changes slightly.",3);
		print_str_color("  It seems to now have more of a green tinge.",3);
		}
	else if ((heal_phase == 2) && (get_health(ME) < (get_max_health(ME) * 1) / 4)) {
		heal_phase = 3;
		print_str_color("Sammann mutters a spell. The color of his skin changes slightly.",3);
		print_str_color("  It seems to now have less of a green tinge.",3);
		}

	if ((((heal_phase == 0) || (heal_phase == 2)) && (dist_to_nav_point(ME,0) > 6)) || (((heal_phase == 1) || (heal_phase == 3)) && (dist_to_nav_point(ME,0) <= 6))) {
		if ((get_health(ME) < get_max_health(ME)) && 
		  (tick_difference(last_heal,get_current_tick()) > 0) && (get_ran(1,0,100) < 90)) {
			last_heal = get_current_tick();
			run_char_animation(2,1,95);	
			pc_heard_sound_delay(100,250);						
			heal_char(ME,heal_level);
			heal_level = heal_level + 300;

			set_char_status(ME,0,12);
			set_char_status(ME,1,12);
			set_char_status(ME,8,12);

			if ((heal_phase == 0) || (heal_phase == 2)) {
				print_named_str(ME,"draws healing power from the sun.");
				if (gf(81,5) == 0) {
					sf(81,5,1);
					begin_talk_mode(20);
					}
				}
				else {
					print_named_str(ME,"draws healing power from the pylons.");
					if (gf(81,6) == 0) {
						sf(81,6,1);
						begin_talk_mode(21);
						}
					}


			end();
			}
			
		}
		else heal_level = 300;
		
	if (used_abil == 0)
		do_attack();
break;

beginstate TALKING_STATE;
	if (get_memory_cell(5) == 0) {
		print_str("Talking: It doesn't respond.");
		}
		else begin_talk_mode(get_memory_cell(5));
break;